package com.javabasic.snmp;

/**
 * @Author xiongmin
 * @Description //TODO
 * @Date 2020/4/16 15:17
 * @Version 1.0
 **/

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import java.io.IOException;
import java.util.List;

public class SnmpSet {

    private Snmp snmp = null;

    private Address targetAddress = null;

    public void initComm() throws IOException {

        // 设置Agent方的IP和端口
        targetAddress = GenericAddress.parse("udp:192.168.1.115/161");
        TransportMapping transport = new
                DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        transport.listen();
    }

    public ResponseEvent sendPDU(PDU pdu) throws IOException {
        // 设置 target
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString("public"));
        target.setAddress(targetAddress);
        // 通信不成功时的重试次数
        target.setRetries(2);
        // 超时时间
        target.setTimeout(1500);
        target.setVersion(SnmpConstants.version1);
        // 向Agent发送PDU，并返回Response
        return snmp.send(pdu, target);
    }

    public void setPDU() throws IOException {
        // set PDU
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.3375.2.2.10.1.2.1.9.25.47.67.111.109.109.111.110.47.84.69.83.84.95.77.79.78.73.84.79.82.95.51.95.86.83"), new Integer32(0)));
        pdu.setType(PDU.SET);
//        sendPDU(pdu);
        readResponse(sendPDU(pdu));
    }

    public void getPDU() throws IOException {
        // get PDU
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.3375.2.2.10.1.2.1.9.25.47.67.111.109.109.111.110.47.84.69.83.84.95.77.79.78.73.84.79.82.95.51.95.86.83")));
        pdu.setType(PDU.GET);
        readResponse(sendPDU(pdu));
    }

    public void readResponse(ResponseEvent respEvnt) {
        // 解析Response
        if (respEvnt != null && respEvnt.getResponse() !=
                null) {
            List<? extends VariableBinding> recVBs =
                    respEvnt.getResponse()
                            .getVariableBindings();
            for (int i = 0; i < recVBs.size(); i++) {
                VariableBinding recVB = recVBs.get(i);
                System.out.println(recVB.getOid() + " : " +
                        recVB.getVariable());
            }
        }
    }

    public static void main(String[] args) {
        try {
            SnmpSet util = new SnmpSet();
            util.initComm();
//            util.getPDU();
//            util.setPDU();
            Thread.sleep(1000);
            util.getPDU();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
        }
    }
}